package com.android.example.myapplication;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

import com.android.example.myapplication.util.PermissionUtil;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btn_share_write).setOnClickListener(this);
        findViewById(R.id.btn_share_read).setOnClickListener(this);
        findViewById(R.id.btn_login_share).setOnClickListener(this);
        findViewById(R.id.btn_sqlite_create).setOnClickListener(this);//btn_sqlite_create创建删除数据库
        findViewById(R.id.btn_sqlite_write).setOnClickListener(this);//btn_sqlite_write写入数据库
        findViewById(R.id.btn_sqlite_read).setOnClickListener(this);//btn_sqlite_read读取数据库
        findViewById(R.id.btn_login_sqlite).setOnClickListener(this);//btn_login_sqlite记住密码
        findViewById(R.id.btn_file_basic).setOnClickListener(this);//btn_file_basic卡SD基本操作
        findViewById(R.id.btn_file_path).setOnClickListener(this);
        findViewById(R.id.btn_text_write).setOnClickListener(this);//btn_text_write写入文本文件
        findViewById(R.id.btn_text_read).setOnClickListener(this);//读取文本文件
        findViewById(R.id.btn_image_write).setOnClickListener(this);//写入图片文件
        findViewById(R.id.btn_image_read).setOnClickListener(this);//读取图片文件
        findViewById(R.id.btn_app_life).setOnClickListener(this);
        findViewById(R.id.btn_app_write).setOnClickListener(this);
        findViewById(R.id.btn_app_read).setOnClickListener(this);
        findViewById(R.id.btn_content_provider).setOnClickListener(this);
        findViewById(R.id.btn_content_resolver).setOnClickListener(this);
        findViewById(R.id.btn_content_observer).setOnClickListener(this);
        findViewById(R.id.btn_menu_option).setOnClickListener(this);
        findViewById(R.id.btn_menu_context).setOnClickListener(this);
        findViewById(R.id.btn_shopping_cart).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_share_write) {
            Intent intent = new Intent(this, ShareWriteActivity.class);
            startActivity(intent);
        } else if (v.getId() == R.id.btn_share_read) {
            Intent intent = new Intent(this, ShareReadActivity.class);
            startActivity(intent);
        } else if (v.getId() == R.id.btn_login_share) {
            Intent intent = new Intent(this, LoginShareActivity.class);
            startActivity(intent);
        } else if (v.getId() == R.id.btn_sqlite_create) {//btn_sqlite_create创建删除数据库
            Intent intent = new Intent(this, DatabaseActivity.class);
            startActivity(intent);
        } else if (v.getId() == R.id.btn_sqlite_write) {//btn_sqlite_write写入数据库
            Intent intent = new Intent(this, SQLiteWriteActivity.class);
            startActivity(intent);
        } else if (v.getId() == R.id.btn_sqlite_read) {//btn_sqlite_read读取数据库
            Intent intent = new Intent(this, SQLiteReadActivity.class);
            startActivity(intent);
        } else if (v.getId() == R.id.btn_login_sqlite) {//btn_login_sqlite记住密码
            Intent intent = new Intent(this, LoginSQLiteActivity.class);
            startActivity(intent);
        } else if (v.getId() == R.id.btn_file_basic) {//btn_file_basic卡SD基本操作
            Intent intent = new Intent(this, FileBasicActivity.class);
            startActivity(intent);
        } else if (v.getId() == R.id.btn_file_path) {//btn_file_path公有和私有空间
            Intent intent = new Intent(this, FilePathActivity.class);
            startActivity(intent);
        } else if (v.getId() == R.id.btn_text_write) {//btn_text_write写入文本文件
            Intent intent = new Intent(this, TextWriteActivity.class);
            startActivity(intent);
        } else if (v.getId() == R.id.btn_text_read) {//读取文本文件
            Intent intent = new Intent(this, TextReadActivity.class);
            startActivity(intent);
        } else if (v.getId() == R.id.btn_image_write) {//写入图片文件
            Intent intent = new Intent(this, ImageWriteActivity.class);
            startActivity(intent);
        } else if (v.getId() == R.id.btn_image_read) {//读取图片文件
            Intent intent = new Intent(this, ImageReadActivity.class);
            startActivity(intent);
        } else if (v.getId() == R.id.btn_app_life) {//生命周期
            Intent intent = new Intent(this, ActJumpActivity.class);
            startActivity(intent);
        } else if (v.getId() == R.id.btn_app_write) {//写入全局变量
            Intent intent = new Intent(this, AppWriteActivity.class);
            startActivity(intent);
        } else if (v.getId() == R.id.btn_app_read) {//读取全局内存
            Intent intent = new Intent(this, AppReadActivity.class);
            startActivity(intent);
        } else if (v.getId() == R.id.btn_content_provider) {//内容提供器
            Intent intent = new Intent(this, ContentProviderActivity.class);
            startActivity(intent);
        } else if (v.getId() == R.id.btn_content_resolver) {//写入联系人
            if (PermissionUtil.checkMultiPermission(this, new String[] {Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS}, R.id.btn_content_resolver % 65536)) {
                startActivity(new Intent(this, ContentResolverActivity.class));
            }
        } else if (v.getId() == R.id.btn_content_observer) {
            if (PermissionUtil.checkMultiPermission(this, new String[] {Manifest.permission.SEND_SMS, Manifest.permission.READ_SMS}, R.id.btn_content_observer % 65536)) {
                startActivity(new Intent(this, ContentObserverActivity.class));
            }
        } else if (v.getId() == R.id.btn_menu_option) {
            Intent intent = new Intent(this, MenuOptionActivity.class);
            startActivity(intent);
        } else if (v.getId() == R.id.btn_menu_context) {
            Intent intent = new Intent(this, MenuContextActivity.class);
            startActivity(intent);
        } else if (v.getId() == R.id.btn_shopping_cart) {
            Intent intent = new Intent(this, ShoppingCartActivity.class);
            startActivity(intent);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == R.id.btn_content_resolver % 65536) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                startActivity(new Intent(this, ContentResolverActivity.class));
            } else {
                Toast.makeText(this, "这个是什么？", Toast.LENGTH_SHORT).show();
            }
        } else if (requestCode == R.id.btn_content_observer % 65536) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                startActivity(new Intent(this, ContentObserverActivity.class));
            } else {
                Toast.makeText(this, "后面的是什么？？？？", Toast.LENGTH_SHORT).show();
            }
        }

    }
}